<template>
  <div class="app-container">
    <el-row>
      <el-col :span="24">
        <div class="h_top">
          <div class="bread_title">
            <span>更新任务组</span>
          </div>
          <div class="save_button">
            <div class="buttonBox">
              <el-button type="primary" icon="el-icon-back" size="mini" @click="goBack">返回</el-button>
            </div>
            <div class="buttonBox">
              <el-button type="primary" icon="el-icon-document-checked" size="mini" @click="save">保存</el-button>
            </div>
            <div class="buttonBox">
              <el-button type="primary" icon="el-icon-document-remove" size="mini" @click="confirmDel">删除</el-button>
            </div>
          </div>
        </div>
      </el-col>
    </el-row>
    <div>
      <el-row>
        <el-col :span="24">
          <el-form ref="form" :model="data" inline :rules="formRules">
            <el-input v-model="data.id" type="hidden" />
            <el-row :gutter="10">
              <el-col :span="4">
                <el-form-item prop="coding">
                  <div>编号</div>
                  <el-input v-model="data.coding" size="mini" />
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item prop="name">
                  <div>任务组名称</div>
                  <el-input v-model="data.name" size="mini" />
                </el-form-item>
              </el-col>
              <el-col :span="3">
                <el-form-item prop="status">
                  <div>状态</div>
                  <el-select v-model="data.status" size="mini">
                    <el-option v-for="(item, index) in commonStatus" :key="index" :value="index" :label="item" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="2">
                <el-form-item prop="sort">
                  <div>排序</div>
                  <el-input v-model="data.sort" size="mini" />
                </el-form-item>
              </el-col>
              <el-col :span="9" hidden>
                <el-form-item>
                  <div>是否同步</div>
                  <el-checkbox v-model="data.is_sync" label="同步任务组的‘新增’或‘删除’到相关维护计划？" :true-label="1" :false-label="0" />
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
          <el-tabs>
            <el-tab-pane label="详细">
              <el-table :data="list">
                <el-table-column label="任务" min-width="26%" prop="task.name" />
                <el-table-column label="任务细节" min-width="20%" prop="task.detail" />
                <el-table-column label="参数" min-width="26%" prop="sparam.name" />
                <el-table-column label="参数标准" min-width="20%" prop="sparam.standard" />
                <el-table-column label="操作" min-width="8%">
                  <template scope="scope">
                    <el-button type="text" size="small" @click="remove(scope.row.id)">删除</el-button>
                  </template>
                </el-table-column>
                <div slot="append">
                  <el-form ref="append_form" :model="task" :inline="true" class="hf_form append_other_admin" :rules="appendRules">
                    <el-row :gutter="10">
                      <el-col :span="11">
                        <el-form-item prop="task_id">
                          <v-select-s v-model="task.task_id" multiple reserve action="taskList" :query="{add_field:'standard'}" />
                        </el-form-item>
                      </el-col>
                      <el-col :span="11">
                        <el-form-item prop="param_id">
                          <v-select-s v-model="task.param_id" action="paramList" :query="{add_field: 'standard'}" />
                        </el-form-item>
                      </el-col>
                      <el-col :span="2">
                        <el-form-item>
                          <el-button icon="el-icon-upload2" size="mini" @click="append" />
                        </el-form-item>
                      </el-col>
                    </el-row>
                  </el-form>
                </div>
              </el-table>
            </el-tab-pane>
            <el-tab-pane label="描述">
              <el-row :gutter="10">
                <el-col :span="4">
                  <div>
                    <span>创建时间日期</span>
                    <el-input v-model="data.create_time" placeholder="创建时间日期" class="mtb10" :disabled="true" size="mini" />
                  </div>
                  <div>
                    <span>创建人</span>
                    <el-input v-model="data.cr_admin.name" placeholder="创建人" class="mtb10" :disabled="true" size="mini" />
                  </div>
                  <div v-if="data.update_time">
                    <div>
                      <span>最近更新</span>
                      <el-input v-model="data.update_time" placeholder="最近更新" class="mtb10" :disabled="true" size="mini" />
                    </div>
                    <div>
                      <span>更新者</span>
                      <el-input v-model="data.up_admin.name" placeholder="更新者" class="mtb10" :disabled="true" size="mini" />
                    </div>
                  </div>
                </el-col>
                <el-col :span="20">
                  <Tinymce id="content" v-model="data.content" />
                </el-col>
              </el-row>
            </el-tab-pane>
          </el-tabs>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
import API from '@/api/project/group'
import { commonStatus } from '@/utils/options'
import VSelect from '@/components/select'
import VSelectS from '@/components/select-search'
import Tinymce from '@/components/Tinymce/index'
export default {
  inject: ['backUrl', 'delOne', 'update', 'appendOtherAdmin', 'delOtherAdmin', 'getWeightFormat', 'goUrl'],
  components: {
    Tinymce,
    VSelect,
    VSelectS
  },
  data() {
    return {
      list: [],
      data: {
        'cr_admin': {
          'name': ''
        }
      },
      task: {
        task_id: []
      },
      commonStatus,
      formRules: {
        coding: [
          {
            required: true,
            message: '请输入编号',
            trigger: 'blur'
          }
        ],
        name: [
          {
            required: true,
            message: '请输入任务组名称',
            trigger: 'blur'
          }
        ],
        status: [
          {
            required: true,
            message: '请选择状态',
            trigger: 'change'
          }
        ],
        sort: [
          {
            required: true,
            message: '请输入排序',
            trigger: 'blur'
          }
        ]
      },
      appendRules: {
        task_id: [
          {
            required: true,
            message: '请选择任务',
            trigger: 'change'
          }
        ],
        param_id: [
          {
            required: true,
            message: '请选择参数',
            trigger: 'change'
          }
        ]
      }
    }
  },
  created() {
    const id = this.$route.params.id
    API.getOne(id).then(response => {
      if (response.data.error_code === 64404) {
        this.$alert('您请求的任务组不存在', '错误', {
          confirmButtonText: '确定',
          callback: () => {
            this.backUrl()
          }
        })
      }
      this.data = response.data
      this.list = response.data.tasks
    })
  },
  methods: {
    confirmDel() {
      const id = this.data.id
      this.delOne(API, id, true)
    },
    save() {
      const thisData = this.data
      const thisForm = this.$refs.form
      this.update(API, thisData, thisForm)
    },
    append() {
      this.$refs.append_form.validate((valid) => {
        if (valid) {
          const task_data = {
            'task_id': this.task.task_id,
            'param_id': this.task.param_id,
            'group_id': this.$route.params.id
          }
          API.append(task_data).then(response => {
            if (response.data.error_code === 1) {
              this.$message({
                message: '恭喜你，更新成功',
                type: 'success'
              })
              API.getOne(this.$route.params.id).then(response => {
                this.list = response.data.tasks
              })
            } else if (response.data.error_code === 64004) {
              this.$message.error('该任务已存在')
            } else {
              this.$message.error(response.data.message)
            }
          })
        }
      })
    },
    remove(id) {
      const task_data = {
        'id': id
      }
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        API.remove(task_data).then((response) => {
          if (response.data.error_code === 1) {
            this.$message({
              message: '恭喜你，删除成功',
              type: 'success'
            })
            API.getOne(this.$route.params.id).then(response => {
              this.list = response.data.tasks
            })
          } else {
            this.$message.error(response.data.message)
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    goBack() {
      this.goUrl('/project/group')
    }
  }
}
</script>
